;(function($){
	$.fn.digitalCalculator = function(opt){
		var el = this;
		var opts = {
			type : "pNum",//pNum:人数;discount:小数折扣;percent:百分比;price:两位小数;
			grade : 1,//调节按钮梯度
			status : true,
			isClear : true,
			title : "",
			getDefaultBack:function(){},//获取默认值完毕回调函数
        	workComplete:function(){},//插件加载完成
    		dataBack:function(){},//完成结账回调函数
    		returnBack:function(){},//返回回调函数
		}
		var param = $.extend(opts,opt);
		param.type = "price";
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			//键盘事件
			var num = ""
			$(document).unbind("keydown").keydown(function(e){
//				console.log(num)
				if(e.keyCode==27){
					param.returnBack();
					el.empty();
					el.hide();
					$(document).unbind("keydown");
				}else if(e.keyCode==13){
					$(document).unbind("keydown");
    				el.find("li.func.sure").find("button").trigger("click");
    			}else if((e.keyCode>=48&&e.keyCode<=57)||(e.keyCode>=96&&e.keyCode<=105)){
    				var ss = num.toString()
    				if(ss.indexOf(".")>-1&&ss.length-ss.indexOf(".")>=3){
    					return false;
    				}
    				if(num){
    					num = num+e.key
    				}else{
    					num = e.key
    				}
    				el.find("#spinner3").val("");
    				el.find("#spinner3").val(num);
    			}else if((e.keyCode==109||e.keyCode==189)&&param.type=="price"){
    				if(num.toString().indexOf("-")>-1){
						num = num.toString().replace("-","");
					}else{
						num = "-" + num;
					}
    				el.find("#spinner3").val(num);
    			}else if(e.keyCode==110){
    				if(num.toString().indexOf(".")==-1){
    					num = num+"."
					}
    				el.find("#spinner3").val("");
    				el.find("#spinner3").val(num);
    			}else if(e.keyCode==8){
    				if(num){
    					num = num.substring(0,num.length-1);
    				}else{
    					num = "";
    				}
    				el.find("#spinner3").val("");
    				el.find("#spinner3").val(num);
    			}
				return false;
			})
			
			//数字键盘点击
    		el.find("li .btn.btn-app").unbind("click").bind("click",function(event){
    			event.stopPropagation();
//    			var num = el.find("#spinner3").val();
    			num=num.toString(); 
    			if(param.isClear){
    				num = "";
    				param.isClear = false;
    			}
    			if(num.indexOf("%")!=-1&&$(this).parent().attr("class")=="num"&&param.type=="percent"){
    				num = "";
    			}
    			
    			if((num.indexOf(".")!=-1||num=="")&&param.type=="price"&&($(this).text()==".00"||$(this).text()==".")){
    				return false;
    			}else if(num==""&&($(this).text()==".00"||$(this).text()==".")){
    				
    			}
    			
				if($(this).parent().hasClass("exit")){
					param.returnBack();
					el.hide();
					$(document).unbind("keydown");
					num = "";
				}else if($(this).parent().hasClass("del")){
					num = num.substring(0,num.length-1);
				}else if($(this).parent().hasClass("clearall")){
					num = "";
					if($(this).parent().hasClass("num")){//为折扣是0.清空已写。
						num = $(this).text();
					}
				}else if($(this).parent().hasClass("sure")){
					num = t.checkTwoPoint(el.find("#spinner3").val());
					if(param.title.substring(param.title.length-2,param.title.length) == "价格"){
						param.dataBack(num||param.number);
						el.hide();
						$(document).unbind("keydown");
					}else{
						param.dataBack(num||param.number);
						el.hide();
						$(document).unbind("keydown");
					}
					return false;
				}else if($(this).parent().hasClass("plus-minus")){//plus-minus
					if(num.toString().indexOf("-")>-1){
						num = num.toString().replace("-","");
					}else{
						num = "-" + num;
					}
				}else{
					if(param.keySta == 0){
						num = $(this).text();		
						param.keySta = 1;
					}else{
						var ss = num.toString()
	    				if(ss.indexOf(".")>-1&&ss.length-ss.indexOf(".")>=3){
	    					return false;
	    				}
						num = num  + $(this).text();						
					}
				}
				if(num == 0 || num < 0){
					el.find("#spinner3").css({"color":"red"});
				}else{
					el.find("#spinner3").css({"color":"#777"});					
				}
				el.find("#spinner3").val(num);
    		});
    		
    		//数字键盘加减
    		el.find(".spinbox-buttons").bind("click",function(event){
				event.stopPropagation();
				var num = el.find("#spinner3").val()*1;
				if($(this).index() == 0){
					if(param.status){						
						num = num - param.grade;
					}else{
						if(num > 1){
							num = num - param.grade;
						}
					}
				}else{
					num = num + param.grade;
				}
				if(num == 0 || num < 0){
					el.find("#spinner3").css({"color":"red"});
				}else{
					el.find("#spinner3").css({"color":"#777"});					
				}
				el.find("#spinner3").val(num);
				param.isClear= false;
			});
    		
    		el.find("#spinner3").focus().select();
//    		el.find("#spinner3").focus(function(){
//    			el.find("#spinner3").blur();
//    		});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var html = t[param.type+"Html"]();
			el.html(html).show();
		},
		pNumHtml : function(){//pNum:人数
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = '<div class="ace-spinner middle touch-spinner title">'+
					   		''+ param.title +''+
					   '</div>'+
				       '<div class="ace-spinner middle touch-spinner">'+
							'<div class="input-group">'+
								'<div class="spinbox-buttons input-group-btn">'+
									'<button type="button" class="btn spinbox-down btn-sm btn-danger">'+
										'<i class="icon-only  ace-icon ace-icon fa fa-minus bigger-110"></i>'+
									'</button>'+
								'</div>'+
								'<input type="text" id="spinner3" class="spinbox-input form-control text-center" value="'+ (param.number||0) +'">'+
								'<div class="spinbox-buttons input-group-btn">'+
									'<button type="button" class="btn spinbox-up btn-sm btn-success">'+
										'<i class="icon-only  ace-icon ace-icon fa fa-plus bigger-110"></i>'+
									'</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<ul class="num-key clearfix">'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">1</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">2</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">3</button>'+
							'</li>'+
							'<li class="func exit">'+
								'<button type="button" class="btn btn-app btn-grey dishes_each">'+$.i18n.propJsp("退出")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">4</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">5</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">6</button>'+
							'</li>'+
							'<li class="func del">'+
								'<button type="button" class="btn btn-app btn-warning dishes_each">'+$.i18n.propJsp("退格")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">7</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">8</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">9</button>'+
							'</li>'+
							'<li class="func clearall">'+
								'<button type="button" class="btn btn-app btn-primary dishes_each">'+$.i18n.propJsp("清除")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">0</button>'+
							'</li>'+
//							'<li class="num">'+
//								'<button type="button" class="btn btn-app btn-light dishes_each">.</button>'+
//							'</li>'+
							'<li class="func sure" style=" float:right;margin-right:30px;">'+
								'<button type="button" class="btn btn-app btn-success dishes_each">'+$.i18n.propJsp("确定")+'</button>'+
							'</li>'+
						'</ul>';
			return html;
		},
		discountHtml : function(){//discount:小数折扣;
			var el = this.el;
			var param = this.param;
			var t = this;
			var html = '<div class="ace-spinner middle touch-spinner title">'+
					   		''+ param.title +''+
					   '</div>'+
				       '<div class="ace-spinner middle touch-spinner">'+
							'<div class="input-group">'+
//								'<div class="spinbox-buttons input-group-btn">'+
//									'<button type="button" class="btn spinbox-down btn-sm btn-danger">'+
//										'<i class="icon-only  ace-icon ace-icon fa fa-minus bigger-110"></i>'+
//									'</button>'+
//								'</div>'+
								'<input type="text" id="spinner3" class="spinbox-input form-control text-center" value="'+ (param.number||0.00) +'" style="width:280px;">'+
//								'<div class="spinbox-buttons input-group-btn">'+
//									'<button type="button" class="btn spinbox-up btn-sm btn-success">'+
//										'<i class="icon-only  ace-icon ace-icon fa fa-plus bigger-110"></i>'+
//									'</button>'+
//								'</div>'+
							'</div>'+
						'</div>'+
						'<ul class="num-key clearfix">'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">1</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">2</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">3</button>'+
							'</li>'+
							'<li class="func exit">'+
								'<button type="button" class="btn btn-app btn-grey dishes_each">'+$.i18n.propJsp("退出")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">4</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">5</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">6</button>'+
							'</li>'+
							'<li class="func del">'+
								'<button type="button" class="btn btn-app btn-warning dishes_each">'+$.i18n.propJsp("退格")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">7</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">8</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">9</button>'+
							'</li>'+
							'<li class="func clearall">'+
								'<button type="button" class="btn btn-app btn-primary dishes_each">'+$.i18n.propJsp("清除")+'</button>'+
							'</li>'+
							'<li class="num clearall">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">0.</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">0</button>'+
							'</li>'+
							'<li class="func sure">'+
								'<button type="button" class="btn btn-app btn-success dishes_each">'+$.i18n.propJsp("确定")+'</button>'+
							'</li>'+
						'</ul>';
			return html;
		},
		percentHtml : function(){//percent:百分比;
			var el = this.el;
			var param = this.param;
			var t = this;
			var html = '<div class="ace-spinner middle touch-spinner title">'+
					   		''+ param.title +''+
					   '</div>'+
				       '<div class="ace-spinner middle touch-spinner">'+
							'<div class="input-group">'+
								'<div class="spinbox-buttons input-group-btn">'+
									'<button type="button" class="btn spinbox-down btn-sm btn-danger">'+
										'<i class="icon-only  ace-icon ace-icon fa fa-minus bigger-110"></i>'+
									'</button>'+
								'</div>'+
								'<input type="text" id="spinner3" class="spinbox-input form-control text-center" value="'+ (param.number||0) +'">'+
								'<div class="spinbox-buttons input-group-btn">'+
									'<button type="button" class="btn spinbox-up btn-sm btn-success">'+
										'<i class="icon-only  ace-icon ace-icon fa fa-plus bigger-110"></i>'+
									'</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<ul class="num-key clearfix">'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">1</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">2</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">3</button>'+
							'</li>'+
							'<li class="func exit">'+
								'<button type="button" class="btn btn-app btn-grey dishes_each">'+$.i18n.propJsp("退出")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">4</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">5</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">6</button>'+
							'</li>'+
							'<li class="func del">'+
								'<button type="button" class="btn btn-app btn-warning dishes_each">'+$.i18n.propJsp("退格")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">7</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">8</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">9</button>'+
							'</li>'+
							'<li class="func clearall">'+
								'<button type="button" class="btn btn-app btn-primary dishes_each">'+$.i18n.propJsp("清除")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">0</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">.</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">%</button>'+
							'</li>'+
							'<li class="func sure" style="width:100px;">'+
								'<button type="button" class="btn btn-app btn-success dishes_each">'+$.i18n.propJsp("确定")+'</button>'+
							'</li>'+
						'</ul>';
			return html;
		},
		priceHtml : function(){//price:两位小数;
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = '<div class="ace-spinner middle touch-spinner title">'+
					   		''+ param.title +''+
					   '</div>'+
				       '<div class="ace-spinner middle touch-spinner">'+
							'<div class="input-group">'+
								'<div class="spinbox-buttons input-group-btn">'+
									'<button type="button" class="btn spinbox-down btn-sm btn-danger">'+
										'<i class="icon-only  ace-icon ace-icon fa fa-minus bigger-110"></i>'+
									'</button>'+
								'</div>'+
								'<input type="text" id="spinner3" class="spinbox-input form-control text-center" value="'+ (param.number||0.00) +'">'+
								'<div class="spinbox-buttons input-group-btn">'+
									'<button type="button" class="btn spinbox-up btn-sm btn-success">'+
										'<i class="icon-only  ace-icon ace-icon fa fa-plus bigger-110"></i>'+
									'</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<ul class="num-key clearfix">'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">1</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">2</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">3</button>'+
							'</li>'+
							'<li class="func exit">'+
								'<button type="button" class="btn btn-app btn-grey dishes_each">'+$.i18n.propJsp("ESC退出")+'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">4</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">5</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">6</button>'+
							'</li>'+
							'<li class="func del">'+
								'<button type="button" class="btn btn-app btn-warning dishes_each" style="display:flex;justify-content:center;">'+
									'<i class="ace-icon fa fa-angle-left" style="zoom: 75%;margin-top: 2px;margin-right: 9px;"></i>'+$.i18n.propJsp("退格")+
								'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">7</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">8</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">9</button>'+
							'</li>'+
							'<li class="func clearall">'+
								'<button type="button" class="btn btn-app btn-primary dishes_each" style="display:flex;justify-content:center;">'+
									'<i class="ace-icon fa fa-trash" style="zoom:47%; margin-top:14px;"></i>'+$.i18n.propJsp("清除")+
								'</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">0</button>'+
							'</li>'+
							'<li class="num">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">.</button>'+
							'</li>'+
							'<li class="plus-minus">'+
								'<button type="button" class="btn btn-app btn-light dishes_each">+/-</button>'+
							'</li>'+
							'<li class="func sure" style="width:100px;">'+
								'<button type="button" class="btn btn-app btn-success dishes_each" style="display:flex;justify-content:center;">'+
									'<i class="ace-icon fa fa-check" style="zoom:47%; margin-top:14px;"></i>'+$.i18n.propJsp("确定")+
								'</button>'+
							'</li>'+
						'</ul>';
			return html;
		},
		//俩位小数校验
    	checkTwoPoint:function(value){
    		var t=this;
//    		var $opt=$t.options;
//    		var el=$t.$element;
    		
    		value=value.toString()+".00";
//    		value = value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
//    		value = value.replace(/^\./g,""); //验证第一个字符是数字
    		value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
    		value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    		value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    		
    		return value;
    	},
	}
	
	//获取项目路径
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}
})(jQuery);